package com.smart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.pojo.Salary;
import com.smart.mapper.SalaryMapper;
import com.smart.req.SalaryReq;
import com.smart.resp.SalaryResp;
import com.smart.service.ISalaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyj
 * @since 2023-05-17
 */
@Service
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, Salary> implements ISalaryService {

    @Override
    public void queryByCondition(SalaryReq salaryReq, Page<SalaryResp> page) {
        QueryWrapper<SalaryResp> wrapper = new QueryWrapper<>();
        wrapper.apply(" s.del_flag=0 ")
                .eq(!ObjectUtils.isEmpty(salaryReq.getStatus()), "s.status", salaryReq.getStatus())
                .and(!ObjectUtils.isEmpty(salaryReq.getKeyword()),
                        qw -> qw.like("u.username", salaryReq.getKeyword())
                                .or()
                                .like("r.name", salaryReq.getKeyword()));
        baseMapper.queryByCondition(wrapper, page);
    }

    @Override
    public List<Salary> queryNotPaySalaries() {
        return baseMapper.queryNotPaySalaries();
    }

    @Override
    public Salary getByUserId(Long userId) {
        List<Salary> salaries = baseMapper.getByUserId(userId);
        if (salaries.isEmpty()) {
            return null;
        } else {
            return salaries.get(0);
        }
    }
}
